import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {AppGlobal, AppService} from "../../../services/app.service";
import {ResDataModel} from "../../../models/res-data-model";

/**
 * Generated class for the SendMailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-send-mail',
  templateUrl: 'send-mail.html',
})
export class SendMailPage {

  mail:string = "";
  seconds:number;
  timer:any;//局部计时器

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private appService:AppService) {
    this.seconds = AppGlobal.seconds;
    if(this.seconds<60){
      //初始化一个定时器，只修改局部变量
      this.timer = setInterval(()=>{
        this.seconds = this.seconds - 1;
        if(this.seconds == 0){
          //重置为60s
          this.seconds = 60;
          clearInterval(this.timer);
        }
      },1000)
    }

  }

  ionViewDidLoad() {
  }

  ionViewDidLeave(){
    //离开页面时清除局部计时器
    clearInterval(this.timer);
  }

  toRegisterPage(){
    event.preventDefault();
    //60s倒计时
    AppGlobal.seconds --;
    this.seconds = AppGlobal.seconds;
    //定时器，修改全局计时变量
    var interval = setInterval(()=>{
      AppGlobal.seconds = AppGlobal.seconds - 1;
      this.seconds = AppGlobal.seconds;
      if(AppGlobal.seconds == 0){
        //重置为60s
        AppGlobal.seconds = 60;
        this.seconds = AppGlobal.seconds;
        clearInterval(interval);
      }
    },1000);

    this.appService.httpPost('AppUserCtrl_sendIdentifyingCodeForRegister',
      {mail:this.mail},null,(res:ResDataModel)=>{
        if(res.datas[0].value=="success"){
          this.navCtrl.push('RegisterPage',{mail:this.mail});
        }
      },false)
  }
}
